//
//  SAPayAuthorizationVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/9/3.
//

#import "SAPayAuthorizationVC.h"
#import "SAAuthorizationListAPI.h"
#import "SAWechatAuthorizationVC.h"
#import "SAAlipayAuthorizationVC.h"

@interface SAPayAuthorizationVC ()<HTTPAPIDelegate>

@property (nonatomic, strong) SAAuthorizationListAPI *authorizationListAPI;
@property (nonatomic, strong) UIView *whiteView2;
@property (nonatomic, strong) UIView *whiteView3;
@property (nonatomic, strong) UIView *whiteView4;

@end

@implementation SAPayAuthorizationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.authorizationListAPI == api) {
        NSInteger wxPayTradePermission = [data[@"data"][@"wxPayTradePermission"] integerValue];
        NSInteger aliPayTradePermission = [data[@"data"][@"aliPayTradePermission"] integerValue];
        if (wxPayTradePermission != 1) {
            self.whiteView3.hidden = NO;
            if (aliPayTradePermission != 1) {
                self.whiteView4.hidden = NO;
                self.whiteView2.hidden = YES;
                self.whiteView4.frame = CGRectMake(0, kScaleNum(StatusHeight + 87), kScreenWidth, kScaleNum(141));
            }else {
                self.whiteView4.hidden = YES;
                self.whiteView2.hidden = NO;
                self.whiteView2.frame = CGRectMake(0, kScaleNum(StatusHeight + 87), kScreenWidth, kScaleNum(111));
            }
        }else {
            if (aliPayTradePermission != 1) {
                self.whiteView4.hidden = NO;
                self.whiteView2.hidden = YES;
                self.whiteView4.frame = CGRectMake(0, kScaleNum(StatusHeight + 57), kScreenWidth, kScaleNum(141));
            }else {
                self.whiteView4.hidden = YES;
                self.whiteView2.hidden = NO;
            }
        }
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
  
}

#pragma mark - event
-(void)clickedAuthorizationBtn {
    SAWechatAuthorizationVC *pushVC = [SAWechatAuthorizationVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedAuthorizationBtn2 {
    SAAlipayAuthorizationVC *pushVC = [SAAlipayAuthorizationVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"支付授权"];
    self.view.backgroundColor = kLineColor;
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 1), kScreenWidth, kScaleNum(55))];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.userInteractionEnabled = NO;
    [self.view addSubview:whiteView];
    
    UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wechatBtn setImage:[UIImage imageNamed:@"authorization_wechat"] forState:UIControlStateNormal];
    wechatBtn.frame = CGRectMake(kScaleNum(20), kScaleNum(0), kScaleNum(150), kScaleNum(55));
    [wechatBtn setTitle:@"  微信" forState:UIControlStateNormal];
    [wechatBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    wechatBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    wechatBtn.titleLabel.font = kFont(15);
    [whiteView addSubview:wechatBtn];
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 57), kScreenWidth, kScaleNum(111))];
    whiteView2.backgroundColor = [UIColor whiteColor];
    whiteView2.userInteractionEnabled = NO;
    [self.view addSubview:whiteView2];
    self.whiteView2 = whiteView2;
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(55), kScreenWidth, kScaleNum(1))];
    line.backgroundColor = kLineColor;
    line.userInteractionEnabled = NO;
    [whiteView2 addSubview:line];
    
    UIButton *alipayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [alipayBtn setImage:[UIImage imageNamed:@"authorization_alipay"] forState:UIControlStateNormal];
    alipayBtn.frame = CGRectMake(kScaleNum(20), kScaleNum(0), kScaleNum(150), kScaleNum(55));
    [alipayBtn setTitle:@"  支付宝" forState:UIControlStateNormal];
    [alipayBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    alipayBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    alipayBtn.titleLabel.font = kFont(15);
    [whiteView2 addSubview:alipayBtn];
    
    UIButton *bankBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bankBtn setImage:[UIImage imageNamed:@"authorization_bank"] forState:UIControlStateNormal];
    bankBtn.frame = CGRectMake(kScaleNum(20), kScaleNum(56), kScaleNum(150), kScaleNum(55));
    [bankBtn setTitle:@"  银联" forState:UIControlStateNormal];
    [bankBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    bankBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    bankBtn.titleLabel.font = kFont(15);
    [whiteView2 addSubview:bankBtn];
    
    //新增微信授权UI
    UIView *whiteView3 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 1), kScreenWidth, kScaleNum(85))];
    whiteView3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView3];
    self.whiteView3 = whiteView3;
    
    UIButton *wechatBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [wechatBtn2 setImage:[UIImage imageNamed:@"authorization_wechat"] forState:UIControlStateNormal];
    wechatBtn2.frame = CGRectMake(kScaleNum(20), kScaleNum(9), kScaleNum(150), kScaleNum(33));
    [wechatBtn2 setTitle:@"  微信" forState:UIControlStateNormal];
    [wechatBtn2 setTitleColor:kBlackColor forState:UIControlStateNormal];
    wechatBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    wechatBtn2.titleLabel.font = kFont(15);
    [whiteView3 addSubview:wechatBtn2];
    
    UILabel *tipLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(62), kScaleNum(40), kScaleNum(250), kScaleNum(43))];
    tipLab.textColor = kLightgGrayColor;
    tipLab.font = kFont(13);
    tipLab.text = @"依照微信官方规定,商户开通支付需\n微信授权成功后方使用。";
    tipLab.textAlignment = NSTextAlignmentLeft;
    tipLab.numberOfLines = 0;
    [whiteView3 addSubview:tipLab];
    
    UILabel *tipLab2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(115), kScaleNum(0), kScaleNum(100), kScaleNum(85))];
    tipLab2.textColor = kBuleColor;
    tipLab2.font = kFont(15);
    tipLab2.text = @"去授权>";
    tipLab2.textAlignment = NSTextAlignmentRight;
    [whiteView3 addSubview:tipLab2];
    
    UIButton *authorizationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    authorizationBtn.frame = CGRectMake(0, 0, kScreenWidth, kScaleNum(85));
    [authorizationBtn addTarget:self action:@selector(clickedAuthorizationBtn) forControlEvents:UIControlEventTouchUpInside];
    [whiteView3 addSubview:authorizationBtn];
    whiteView3.hidden = YES;
    
    //新增支付宝授权UI
    UIView *whiteView4 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 1), kScreenWidth, kScaleNum(141))];
    whiteView4.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView4];
    self.whiteView4 = whiteView4;
    
    UIButton *alipayBt2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [alipayBt2 setImage:[UIImage imageNamed:@"authorization_alipay"] forState:UIControlStateNormal];
    alipayBt2.frame = CGRectMake(kScaleNum(20), kScaleNum(0), kScaleNum(150), kScaleNum(55));
    [alipayBt2 setTitle:@"  支付宝" forState:UIControlStateNormal];
    [alipayBt2 setTitleColor:kBlackColor forState:UIControlStateNormal];
    alipayBt2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    alipayBt2.titleLabel.font = kFont(15);
    [whiteView4 addSubview:alipayBt2];
    
    UILabel *tipLab3 = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(62), kScaleNum(40), kScaleNum(280), kScaleNum(43))];
    tipLab3.textColor = kLightgGrayColor;
    tipLab3.font = kFont(13);
    tipLab3.text = @"依照支付宝官方规定,商户开通支付需\n支付宝授权成功后方使用。";
    tipLab3.textAlignment = NSTextAlignmentLeft;
    tipLab3.numberOfLines = 0;
    [whiteView4 addSubview:tipLab3];
    
    UILabel *tipLab4 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(115), kScaleNum(0), kScaleNum(100), kScaleNum(85))];
    tipLab4.textColor = kBuleColor;
    tipLab4.font = kFont(15);
    tipLab4.text = @"去授权>";
    tipLab4.textAlignment = NSTextAlignmentRight;
    [whiteView4 addSubview:tipLab4];
    
    UIButton *authorizationBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    authorizationBtn2.frame = CGRectMake(0, 0, kScreenWidth, kScaleNum(85));
    [authorizationBtn2 addTarget:self action:@selector(clickedAuthorizationBtn2) forControlEvents:UIControlEventTouchUpInside];
    [whiteView4 addSubview:authorizationBtn2];
    whiteView4.hidden = YES;
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(85), kScreenWidth, kScaleNum(1))];
    line2.backgroundColor = kLineColor;
    line2.userInteractionEnabled = NO;
    [whiteView4 addSubview:line2];
    
    UIButton *bankBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [bankBtn2 setImage:[UIImage imageNamed:@"authorization_bank"] forState:UIControlStateNormal];
    bankBtn2.frame = CGRectMake(kScaleNum(20), kScaleNum(86), kScaleNum(150), kScaleNum(55));
    [bankBtn2 setTitle:@"  银联" forState:UIControlStateNormal];
    [bankBtn2 setTitleColor:kBlackColor forState:UIControlStateNormal];
    bankBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    bankBtn2.titleLabel.font = kFont(15);
    [whiteView4 addSubview:bankBtn2];
    
    
    [self.authorizationListAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(SAAuthorizationListAPI *)authorizationListAPI {
    if (!_authorizationListAPI) {
        _authorizationListAPI = [SAAuthorizationListAPI apiWithDelegate:self];
    }
    return _authorizationListAPI;
}

@end
